import Vue from "vue";
import $ from "jquery";
import com from "../../utils/common";
import Loading from "./loading";
import weShare from "./weshare";
let sharePop = {};
sharePop.Temp = `
<div class="mask share_mask">
 <!-- 分享标题以及副标题处弹窗 -->
  <div class="share_pop">
          <div class="share_pop_content">
              <p>分享后朋友们看见的标题、图标和描述.</p>
              <p>
                  <input class="share_title" v-model="sharemsg.ShareTitle">
              </p>
              <div>
                  <div class="left_img">
                      <img :src="sharemsg.ShareImg" alt="">
                      <div>更换图片
                          <form class="shareUp">
                              <input type="file"
                                     id="inputfile"
                                     name="files"
                                     class="share-up-img"
                                     accept="image/jpg,image/jpeg,image/png,image/gif"
                                     @change="uploadImage">
                          </form>
                      </div>
                  </div>
                  <div class="right_word">
                      <textarea v-model="sharemsg.ShareContent"></textarea>
                  </div>
              </div>
          </div>
          <div class="share_footer">
              <div class="share_footer_content">
                  <a href="javascript:;" class="cancel" v-on:click="saveShare(0)">暂不分享</a>
                  <a href="javascript:;" v-on:click="saveShare(1)">保存并分享</a>
              </div>
          </div>
      </div>
</div>
       
`;
let SharePop = Vue.extend({
  template: sharePop.Temp,
  _actid: com.getUrlParam("actid") ? com.getUrlParam("actid") : 1,
  props: ["sharemsg"],
  methods: {
    //上传图像
    uploadImage: function () {
      let self = this;
      let inputDom = $("#inputfile");
      console.log(inputDom[0].files)
      let formData = new FormData();
      formData.append("file", inputDom[0].files[0]);
      Loading.mask();
      com.UploadOneFile(formData, function (data) {
        console.log(data);
        let imgData = data.body;
        console.log(imgData);
        for (let Key in imgData) {
          self.sharemsg.ShareImg = imgData[Key];
        }
        Loading.unmask();
      });
    },
    saveShare: function (status) {
      let Actid = com.getUrlParam("actid") ? com.getUrlParam("actid") : 1;
      console.log(Actid);
      let selfApp = this;
      let shareData = {
        ShareImg: selfApp.sharemsg.ShareImg,
        ShareTitle: selfApp.sharemsg.ShareTitle,
        ShareContent: selfApp.sharemsg.ShareContent,
        Actid: Actid,
      }
      if(status){
        $(".share-way,.mask").show();
      }
      com.ajax({
        url: "/activity/saveShareMsg",
        data: shareData,
        method: "post",
        ok: function (data) {
          if (data["msg"] == "ok") {
            selfApp._wxshare(shareData)
            if(status){
              $('.mask').hide()
              $('.notice_msg').show()
              com.showCenter($(".share-way"))
            }else{
              $('.mask').hide()
            }
          }
        }
      })
    },
    _wxshare: function (obj) {
      let shareObj = obj;
      let url = location.href.split('#')[0];
      com.ajax({
        url: "/wx/getjsticket",
        data: {
          reqUrl: url,
        },
        ok: function (data) {
          $.extend(shareObj, data["msg"])
          console.log(shareObj);
          weShare(shareObj)
          $(".share_pop").hide();
        }
      })
    }
  }
});
export default SharePop;
